package com.gill.controller.test;

import com.gill.entity.test.TestJson;
import com.gill.entity.test.TestView;
import org.sampleSpringFramework.core.annotation.Controller;
import org.sampleSpringFramework.mvc.annotation.RequestMapping;
import org.sampleSpringFramework.mvc.annotation.RequestParam;
import org.sampleSpringFramework.mvc.annotation.ResponseBody;
import org.sampleSpringFramework.mvc.common.ModelAndView;
import org.sampleSpringFramework.mvc.common.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/test")
public class MVCController {

	/**
	 * 测试目的：测试程序能否正常处理，包括以下步骤
	 * 1、启动tomcat
	 * 2、前端访问：http://localhost:8080/my-springframework/test/method1
	 * 3、
	 */
	@RequestMapping(value = "/method1",REQUEST_METHOD = RequestMethod.GET)
	public void method1(@RequestParam(value = "name") String name){
		System.out.println("method1被访问了,name="+name);
	}

	/**
	 * 测试目的：测试程序能否正常处理无参方法
	 */
	@RequestMapping(value = "/method2",REQUEST_METHOD = RequestMethod.GET)
	public void method2(){
		System.out.println("method2被访问了");
	}

	/**
	 * 测试目的：测试InternalErrorResultRender是否能够渲染成功
	 */
	@RequestMapping(value = "/method3",REQUEST_METHOD = RequestMethod.GET)
	public void method3(){
		System.out.println("method3被执行，发生异常");
		throw new RuntimeException("method3被访问，发生异常");
	}

	/**
	 * 测试目的：测试能否正常返回json数据到客户端
	 */
	@RequestMapping(value = "/method4",REQUEST_METHOD = RequestMethod.GET)
	@ResponseBody
	public Object method4(){
		List<TestJson> result = new ArrayList<>();
		result.add(new TestJson("属性1","属性2","属性3"));
		result.add(new TestJson("属性11","属性22","属性33"));
		result.add(new TestJson("属性111","属性222","属性333"));
		return result;
	}

	/**
	 * 测试目的：测试能否正常返回ModelAndView视图到客户端
	 */
	@RequestMapping(value = "/method5",REQUEST_METHOD = RequestMethod.POST)
	public ModelAndView method5(@RequestParam(value = "name") String name, @RequestParam(value = "sex") String sex){
		// 测试：收到的参数
		TestView data = new TestView(name,sex);
		// 1、随便写个结果
		String result = "后端服务能够正常接收到前端请求：参数<name,sex> =><"+name+","+sex+">";
		// 2、构造ModelAndView实例
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewPath("test.jsp").addModel("result",result);
		return modelAndView;
	}
}
